﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; //一定要加入


[StructLayout(LayoutKind.Sequential, Size = 320)]
/// <summary>
/// 消息结构体
/// </summary>
internal struct SMSMessageStruct
{
    //短信内容
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public byte[] Msg;//char Msg[256]
    //对方手机号码
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    public byte[] PhoneNo;
    //接收时间
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    public byte[] ReceTime;
}

[StructLayout(LayoutKind.Sequential, Size = 296)]
/// <summary>
/// 消息状态报告结构体
/// </summary>
internal struct SMSReportStruct
{
    //短消息编号:index，从0开始递增
    public uint index;
    //短信内容
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public byte[] Msg;
    //是否发送成功 0为失败，非0为成功
    public int Success;
    //目标手机号码
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
    public byte[] PhoneNo;
}

internal class SMS
{
    //启动服务
    [DllImport(@"SMSDLL.dll")]
    public static extern int SMSStartService(int nPort, uint BaudRate, int Parity, int DataBits, int StopBits, int FlowControl, byte[] csca);

    //发送短消息
    [DllImport(@"SMSDLL.dll")]
    public static extern uint SMSSendMessage(byte[] Msg, byte[] PhoneNo);

    //接收短消息
    [DllImport(@"SMSDLL.dll")]
    public static extern int SMSGetNextMessage(out SMSMessageStruct Msg);

    //查询发送状态报告
    [DllImport(@"SMSDLL.dll")]
    public static extern int SMSReport(ref SMSReportStruct rept);

    //停止服务
    [DllImport(@"SMSDLL.dll")]
    public static extern int SMSStopSerice();

    //最近一次错误
    [DllImport(@"SMSDLL.dll")]
    public static extern int SMSGetLastError(string err);

}

